package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session_beans.AirTransportSessionBean;
import session_beans.AirTransportSessionRemote;

public class ReservationServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	private static final long serialVersionUID = 8037874330976675814L;

	private AirTransportSessionRemote airTransportSession;
	
	@Override
	public void init() throws ServletException {
		super.init();
		
		airTransportSession = new AirTransportSessionBean();
	}
	
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String idParam = request.getParameter("flightid");
		String jmbgParam = request.getParameter("jmbg");
		String nameParam = request.getParameter("name");
		String surnameParam = request.getParameter("surname");


		if (idParam != null && jmbgParam != null &&
				nameParam != null && surnameParam != null &&
			idParam.length() > 0 && jmbgParam.length() > 0 &&
			nameParam.length() > 0 && surnameParam.length() > 0) {
			
			int returnFlag = airTransportSession.reserve(idParam, jmbgParam, nameParam, surnameParam);
			
			String message = "";
			
			switch (returnFlag) {
				case 0:
					message = "Invalid flight selected";
					break;
				case 1:
					message = "Reservation time expired";
					break;
				case 2:
					message = "No free seats on this flight";
					break;
				case 3:
				case 4:
					message = "Wrong JMBG";
					break;
				case 5:
					message = "The passenger has already reserved seat on this flight";
					break;
				case 6:
					message = "Intersection of flight time";
					break;
				case 7:
					message = "The seat is successfully reserved";
					break;
			}
			
			response.sendRedirect("viewer/reservation_result.jsp?flightid=" + idParam + "&" + "pass=" + nameParam + " " + surnameParam + "&" + "message=" + message + "");
			return;

		}
		else {
			response.sendRedirect("viewer/main.jsp");
		}
		
	}
	
}
